/* fontInfo bookmarklet */
function FontInfo(element) {
	this.e=element;
};
FontInfo.prototype.e;

FontInfo.prototype.trim = function (str, charlist) {
	str=parseFloat(str);
	if (str==0) return 0;
	if (str%10==0) return str;
	str=str.toFixed(4);
	charlist = !charlist ? ' \.0\s\xA' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
	var re = new RegExp('[' + charlist + ']+$', 'g');
	return str.replace(re, '');
};

FontInfo.prototype.hexnum = function (string, tohex, returnAsFormattedString) {
	string+='';
	if (string=='transparent') return string;
	if(string.indexOf('rgb')==0){tohex=true;};
	if(string.indexOf('#')==0){tohex=false;};
	string=string.replace(/#/g, '');
	string=string.replace(/^\s*rgb/i, ' ');
	string=string.replace(/[,\(\)]/g, ' ');
	string=string.replace(/\s{2,}/g, ' ');
	string=string.replace(/^[\s\b]+|[\s\b]+$/g, '');
	var output=[];
	if(tohex){
		string=string.split(' ');
		for(var s=0; s<string.length; s++){
			string[s]=parseInt(string[s]);
			var L=++output.length-1;
			output[L]=string[s].toString(16);
			if(output[L].length==1){output[L]=output[L]+output[L];};
		}
	}
	else{
		string=string.replace(/\s+/g, '');
		var three=(string.length<=3);
		var increment=(!three || string.length==2)?2:1;
		for(var s=0; s<string.length; s+=increment){
			output[++output.length-1]=(!three)?
			parseInt(string.substring(s,s+2), 16):
			parseInt(string.substring(s,s+1)+string.substring(s,s+1), 16);
		}
	};
	if(!returnAsFormattedString){return output;}
	else if(tohex){output='#'+output.join('');}
	else{output='rgb('+output.join(',')+')';};
	return output;
};

FontInfo.prototype.getInfo = function (property) {
	value = document.defaultView.getComputedStyle(this.e, null).getPropertyValue(property);
	if (parseFloat(value)==0) return "0";
	else return value;
};

FontInfo.prototype.color = function() {
	return this.hexnum(this.getInfo('color'),1,1);
};

FontInfo.prototype.backgroundColor = function() {
	return this.hexnum(this.getInfo('background-color'),1,1);
};

FontInfo.prototype.font = function() {
	return this.fontStyle()+" "+this.fontVariant()+" "+this.fontWeight()+" "+this.fontSize()+"/"
	+this.lineHeight()+" "+this.fontFamily();
};

FontInfo.prototype.borderTop = function() {
	return this.getInfo('border-top-width') + ' ' + this.getInfo('border-top-style') + ' '
	+ this.hexnum(this.getInfo('border-top-color'),1,1);
};

FontInfo.prototype.borderRight = function() {
	return this.getInfo('border-right-width') + ' ' + this.getInfo('border-right-style') + ' '
	+ this.hexnum(this.getInfo('border-right-color'),1,1);
};

FontInfo.prototype.borderBottom = function() {
	return this.getInfo('border-bottom-width') + ' ' + this.getInfo('border-bottom-style') + ' '
	+ this.hexnum(this.getInfo('border-bottom-color'),1,1);
};

FontInfo.prototype.borderLeft = function() {
	return this.getInfo('border-left-width') + ' ' + this.getInfo('border-left-style') + ' '
	+ this.hexnum(this.getInfo('border-left-color'),1,1);
};

FontInfo.prototype.whiteSpace = function() {
	ws=this.getInfo('whiteSpace');
	if(ws!='') return ws;
	else return 'normal';
};

FontInfo.prototype.fontFamily = function() {
	return this.getInfo('font-family');
};

FontInfo.prototype.fontSize = function() {
	return this.getInfo('font-size');
};

FontInfo.prototype.fontSizeAdjust = function() {
	return this.getInfo('font-size-adjust');
};

FontInfo.prototype.fontSizeEm = function() {
	pfs= parseFloat(this.parentFontSize());
	fs = parseFloat(this.fontSize());
	fsem = this.trim(fs/pfs);
	return fsem+"em";
};

FontInfo.prototype.parentFontSize = function() {
	parent = this.e.parentNode;
	return document.defaultView.getComputedStyle(parent, null).getPropertyValue('font-size');
};

FontInfo.prototype.fontWeight = function() {
	return this.getInfo('font-weight');
};

FontInfo.prototype.fontVariant = function() {
	return this.getInfo('font-variant');
};

FontInfo.prototype.fontSize = function() {
	return this.getInfo('font-size');
};
FontInfo.prototype.fontStyle = function() {
	return this.getInfo('font-style');
};

FontInfo.prototype.fontStretch = function() {
	fs = this.getInfo('font-stretch');
	if (fs!='') return fs;
	else return 'normal';
};

FontInfo.prototype.fontSizeAdjust = function() {
	return this.getInfo('font-size-adjust');
};

FontInfo.prototype.letterSpacing = function() {
	return this.getInfo('letter-spacing');
};

FontInfo.prototype.tracking = function() {
	return this.getInfo('letter-spacing');
};

FontInfo.prototype.unicodeBidi = function() {
	return this.getInfo('unicode-bidi');
};

FontInfo.prototype.direction = function() {
	return this.getInfo('direction');
};

FontInfo.prototype.wordSpacing = function() {
	return this.getInfo('word-spacing');
};

FontInfo.prototype.textTransform = function() {
	return this.getInfo('text-transform');
};

FontInfo.prototype.textIndent = function() {
	return this.getInfo('text-indent');
};

FontInfo.prototype.lineHeight = function() {
	return this.getInfo('line-height');
};

FontInfo.prototype.margins = function() {
	return this.getInfo('margin-top') + " " + this.getInfo('margin-right') + " " + this.getInfo('margin-bottom') + " " + this.getInfo('margin-left');
};

FontInfo.prototype.paddings = function() {
	return this.getInfo('padding-top') + " " + this.getInfo('padding-right') + " " + this.getInfo('padding-bottom') + " " + this.getInfo('padding-left');
};

FontInfo.prototype.element = function() {
	el=this.e.tagName;
	if (this.e.id) el=el+"#"+this.e.id;
	if (this.e.className) el=el+" .s"+this.e.className;
	return el;
};

FontInfo.prototype.show = function() {

	p=document.getElementById('taat-font-info-pre');
	if (p) p.parentNode.removeChild(p);
	if (this.e!=p) {
		body = document.getElementsByTagName('body')[0];
		pre = document.createElement('pre');
		pre.setAttribute('id',"taat-font-info-pre");
		pre.innerHTML="TAAT fontInfo\n\n"+this.element() + "\n{\nfont: "+this.fontUsed()+";\n"
		+"font: " + this.font() + ";\n"
		+"font-size: " + this.fontSize() + ";\n"
		+"font-family: " + this.fontFamily() + ";\n"
		+"line-height: " + this.lineHeight() + ";\n"
		+"font-weight: " + this.fontWeight() + ";\n"
		+"font-style: " + this.fontStyle() + ";\n"
		+"font-variant: " + this.fontVariant()+ ";\n"
		+"text-transform: " + this.textTransform()+ ";\n"
		+"text-indent: " + this.textIndent()+ ";\n"
		+"word-spacing: " + this.wordSpacing()+ ";\n"
		+"letter-spacing: " + this.letterSpacing()+ ";\n"
		+"font-stretch: " + this.fontStretch()+ ";\n"
		+"font-size-adjust: " + this.fontSizeAdjust()+ ";\n"
		+"white-space: " + this.whiteSpace()+ ";\n"
		+"unicode-bidi: " + this.unicodeBidi()+ ";\n"
		+"direction: " + this.direction()+ ";\n"
		+"color: " + this.color() + ";\n"
		+"background-color: " + this.backgroundColor() + ";\n"
		+"border-top: " + this.borderTop()+ ";\n"
		+"border-right: " + this.borderRight()+ ";\n"
		+"border-bottom: " + this.borderBottom()+ ";\n"
		+"border-left: " + this.borderLeft()+ ";\n"
		+"margin: " + this.margins()+ ";\n"
		+"padding: " + this.paddings()+ ";\n}"
		;
		pre.style.textAlign='left';
		pre.style.position='fixed';
		pre.style.fontFamily='consolas, "andale mono", monospace';
		pre.style.fontWeight='bold';
		pre.style.background='#ce1126';
		pre.style.MozOpacity='0.9';
		pre.style.padding="9px";
		pre.style.color="white";
		pre.style.fontSize='12px';
		pre.style.left='0';
		pre.style.top='0';
		pre.style.zIndex='200';
		div=document.createElement('ul');
		div.id="taat-font-info-ul";
		div.style.listStyle='none';
		div.style.marginLeft='0';
		div.style.paddingLeft='0';
		li1=document.createElement('li');
		li1.style.display='inline';
		li1.style.marginLeft='0';
		li1.style.paddingLeft='0';

		li2=document.createElement('li');
		li2.style.display='inline';
		off=document.createElement('a');
		off.id='taat-font-info-off';
		off.style.background='white';
		off.style.color='#ce1126';
		off.style.padding='3px';
		off.style.margin='5px';
		off.style.marginLeft='0';
		off.style.textDecoration='underlline';
		off.style.display='block';
		off.style.width='95%';
		off.style.textAlign='center';
		off.innerHTML='Disable';
		hide=document.createElement('a');
		hide.id='taat-font-info-hide';
		hide.style.background='white';
		hide.style.color='#ce1126';
		hide.style.padding='3px';
		hide.style.margin='5px';
		hide.style.marginLeft='0';
		hide.style.width='95%';
		hide.style.textAlign='center';
		hide.style.display='block';
		hide.style.textDecoration='underlline';
		li2.style.marginLeft='0';
		li2.style.paddingLeft='0';
		hide.innerHTML='Hide';
		li1.appendChild(hide);
		li2.appendChild(off);
		div.appendChild(li1);
		div.appendChild(li2);
		pre.appendChild(div);
		body.appendChild(pre);

	};
};

FontInfo.prototype.fontUsed = function() {
	var fontFamily = document.defaultView.getComputedStyle(this.e, null).getPropertyValue("font-family");

	var body = document.getElementsByTagName("body")[0];

	var s = document.createElement("span");
	s.innerHTML = "mmmmmlmmmmm";
	s.style.fontFamily = 'sans-serif';
	s.style.fontSize = "72px";
	s.style.visibility = "hidden";

	var d = document.createElement("div");
	d.style.fontFamily = "sans-serif";
	d.style.fontSize = "72px";
	d.style.width = "100ex";
	d.style.height = "100ex";
	d.style.visibility = "hidden";

	body.appendChild(s);
	body.appendChild(d);

	var s_oryginal_height = s.offsetHeight;
	var s_oryginal_width = s.offsetWidth;
	var d_oryginal_height = d.offsetHeight;
	var d_oryginal_width = d.offsetWidth;

	body.removeChild(s);
	body.removeChild(d);
	var fonts = fontFamily.split(',');
	var f = "";
	detectedFont = "sans-serif";

	for (var i=0;i<fonts.length;i++) {
		f = fonts[i];
		if (f=="arial")
		{
			detectedFont="arial";
			break;
		};
		s.style.fontFamily = f + ", sans-serif";
		body.appendChild(s);
		body.appendChild(d);
		s_new_height = s.offsetHeight;
		s_new_width = s.offsetWidth;
		d_new_height = d.offsetHeight;
		d_new_width = d.offsetWidth;
		body.removeChild(s);
		body.removeChild(d);

		if (s_oryginal_height!=s_new_height
			|| s_oryginal_width!=s_new_width
			|| d_oryginal_height!=d_new_height
			|| d_oryginal_width!=d_new_width) {
		detectedFont = f;
		break;
			};
	};

	return detectedFont;
};


var addEvent = function(instance, eventName, listener) {
	var listenerFn = listener;
	if (instance.addEventListener) {
		instance.addEventListener(eventName, listenerFn, false);
	} else if (instance.attachEvent) {
		listenerFn = function() {
			listener(window.event);
		};
		instance.attachEvent("on" + eventName, listenerFn);
	} else {
		throw new Error("Event registration is not supported by this browser.\n Please use modern browsers.");
	};
	return {
		instance: instance,
		name: eventName,
		listener: listenerFn
	};
};

var removeEvent = function(event) {
	var instance = event.instance;
	if (instance.removeEventListener) {
		instance.removeEventListener(event.name, event.listener, false);
	} else if (instance.detachEvent) {
		instance.detachEvent("on" + event.name, event.listener);
	};
};

var which = function (e) {
	var targ;
	if (!e) {var e=window.event;};
	if (e.target) {targ=e.target;}
	else if (e.srcElement) {targ=e.srcElement;}
	if (targ.nodeType==3) {targ = targ.parentNode;}
	var tname;
	tname=targ.tagName;
	return targ;
};


var stop = function(event) {
	if(!event) var event = window.event;
	event.cancelBubble = true;
	event.returnValue = false;
	if (event.stopPropagation) {
		event.stopPropagation();
		event.preventDefault();
	}
	return false;
};
var nfoStart = function(event) {
	el=which(event);
	p=document.getElementById('taat-font-info-pre');
	ul=document.getElementById('taat-font-info-ul');
	off=document.getElementById('taat-font-info-off');
	hide=document.getElementById('taat-font-info-hide');
	if (el==hide)
	{
		p.parentNode.removeChild(p);
		stop(event);
		return false;
	}
	else if (el==off) {
		p.parentNode.removeChild(p);
		removeEvent(nfoListener);
		stop(event);
		return false;
	}
	else if (el==p || el==ul) {return false;}
	fi=new FontInfo(el);
	fi.show();
	stop(event);
	return false;
};

var nfoListener;
	var body = document.getElementsByTagName("body")[0];
	nfoListener = addEvent(body, "click", nfoStart);
